﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;

using LimitedScopeTrader.Data;

namespace LimitedScopeTrader
{
    public partial class frmChart : LSTFormBase
    {
        public frmChart()
        {
            InitializeComponent();

            List<string> securities = (from tickers in this.businessLayer.GetSecurities()
                                       select tickers.Ticker).ToList();

            this.cmbTicker.DataSource = securities;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            graphPriceHistory.GraphPane.Title.Text = "Status"; //title
            graphPriceHistory.GraphPane.XAxis.Title.Text = "Time"; //X-axis name
            graphPriceHistory.GraphPane.YAxis.Title.Text = "Price"; //Y-axis name

            graphAccount.GraphPane.Title.Text = "Pie Chart";
            graphAccount.GraphPane.XAxis.IsVisible = false;
            graphAccount.GraphPane.YAxis.IsVisible = false;
        }

        private void btnGetPriceData_Click(object sender, EventArgs e)
        {
            DateTime start = DateTime.MaxValue;
            DateTime.TryParse(this.txtStart.Text, out start);
            DateTime end = DateTime.MinValue;
            DateTime.TryParse(this.txtEnd.Text, out end);

            string ticker = this.cmbTicker.Text;

            this.DoGetPriceHistoryGraph(ticker, start, end);            
        }

        private void DoGetPriceHistoryGraph(string ticker, DateTime start, DateTime end)
        {
            GraphPane myPane = graphPriceHistory.GraphPane;
            graphPriceHistory.GraphPane.CurveList.Clear();//clear the previous chart            

            List<StocksDataSet.PricesRow> prices = businessLayer.GetPrices(ticker, start, end);
            string[] xTitle = (from p in prices
                               select p.TradeDate.ToShortDateString()).ToArray<string>();

            double[] PC = (from p in prices
                           select p.Price).ToArray<double>();

            LineItem myCurve;

            #region line

            myCurve = myPane.AddCurve("PC", null, PC, Color.Red);
            myCurve.Line.Fill = new Fill();

            #endregion

            myPane.XAxis.Scale.TextLabels = xTitle;
            myPane.XAxis.Type = AxisType.Text;
            myPane.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 166), 90F);
            myPane.Fill = new Fill(Color.FromArgb(250, 250, 255));
            graphPriceHistory.AxisChange();
            graphPriceHistory.Refresh();
        }

        private void btnGetAccountData_Click(object sender, EventArgs e)
        {
            graphAccount.GraphPane.CurveList.Clear();//clear the previous chart

            GraphPane myPane2 = graphAccount.GraphPane;
            string acctId = this.txtAccountId.Text;
            StocksDataSet.GetAccountPortfolioDataTable portfolio = this.businessLayer.GetPortfolio(acctId);

            string[] Ticker = (from p in portfolio
                               select p.Ticker).ToArray();

            double[] Tickervalue = (from p in portfolio
                                    select p.SumOfQuantity).ToArray();

            myPane2.AddPieSlices(Tickervalue, Ticker); 

            myPane2.Chart.Fill = new Fill(Color.White, Color.FromArgb(255, 255, 166), 90F);
            myPane2.Fill = new Fill(Color.FromArgb(250, 250, 255));
            graphAccount.AxisChange();
            graphAccount.Refresh();
        }

        /// <summary>
        /// Button click event handler to close the form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            graphPriceHistory.GraphPane.CurveList.Clear();//clear the previous chart
            graphPriceHistory.AxisChange();
            graphPriceHistory.Refresh();

            graphAccount.GraphPane.CurveList.Clear();
            graphAccount.AxisChange();
            graphAccount.Refresh();
        }

    }
}
